package com.hzp.tool.common.util;

import java.awt.Component;
import java.awt.Desktop;
import java.awt.Desktop.Action;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.net.URI;

import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.filechooser.FileNameExtensionFilter;

public class UIUtils {
	public static void browseUrl(String url) {
		URI uri = URI.create(url);
		Desktop desk = Desktop.getDesktop();
		if (desk.isSupported(Action.BROWSE)) {
			try {
				desk.browse(uri);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void centerComponent(Component parent, Component comp) {
		Rectangle b = parent.getBounds();
		Dimension d = comp.getSize();
		int x = b.x + (b.width - d.width) / 2;
		int y = b.y + (b.height - d.height) / 2;
		comp.setLocation(x, y);
	}

	public static void centerComponentOnScreen(Component comp) {
		Dimension s = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension d = comp.getSize();
		int x = (s.width - d.width) / 2;
		int y = (s.height - d.height) / 2;
		comp.setLocation(x, y);
	}

	public static File selectFolder(Component parent, String title, String oldPath) {
		JFileChooser chooser = new JFileChooser();
		chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		chooser.setDialogTitle(title);
		if (oldPath != null && oldPath.length() > 0) {
			File curr = new File(oldPath);
			if (curr.exists() && curr.isFile()) {
				curr = curr.getParentFile();
			}
			while(curr != null && !curr.exists()) {
				curr = curr.getParentFile();
			}
			chooser.setCurrentDirectory(curr);
		}
		int ret = chooser.showOpenDialog(parent);
		if (ret == JFileChooser.APPROVE_OPTION) {
			return chooser.getSelectedFile();
		}
		return null;
	}

	public static File selectFile(Component parent, String title, String oldPath, String filterName,
			String... extensions) {
		JFileChooser chooser = new JFileChooser();
		chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		if (oldPath != null) {
			File curr = new File(oldPath);
			if (curr.isFile()) {
				chooser.setSelectedFile(curr);
			} else {
				chooser.setCurrentDirectory(curr.getParentFile());
			}
		}
		if (filterName != null) {
			chooser.setFileFilter(new FileNameExtensionFilter(filterName, extensions));
		}
		chooser.setDialogTitle(title);
		int ret = chooser.showOpenDialog(parent);
		if (ret == JFileChooser.APPROVE_OPTION) {
			return chooser.getSelectedFile();
		}
		return null;
	}

	public static File selectFile(Component parent, String title, String oldPath) {
		return selectFile(parent, title, oldPath, null);
	}

	public static void fixComponentSize(Component comp, Dimension size) {
		comp.setPreferredSize(size);
		comp.setMinimumSize(size);
		comp.setMaximumSize(size);
	}

	public static void fixComponentSize(Component comp, int width, int height) {
		fixComponentSize(comp, new Dimension(width, height));
	}

	public static void fixComponentsSize(int width, int height, Component... comps) {
		Dimension size = new Dimension(width, height);
		for (Component comp : comps) {
			fixComponentSize(comp, size);
		}
	}

	public static JScrollPane wrapWithScroll(Component comp) {
		return wrapWithScroll(comp, null);
	}

	public static JScrollPane wrapWithScroll(Component comp, int width, int height) {
		return wrapWithScroll(comp, new Dimension(width, height));
	}

	public static JScrollPane wrapWithScroll(Component comp, Dimension size) {
		JScrollPane scroll = new JScrollPane();
		scroll.setViewportView(comp);
		if (size != null) {
			scroll.setPreferredSize(size);
		}
		return scroll;
	}

	public static JLabel newLabel(String str, int width, int height) {
		JLabel label = new JLabel(str);
		label.setHorizontalAlignment(JLabel.RIGHT);
		if (width > 0 && height > 0) {
			fixComponentSize(label, width, height);
		}
		return label;
	}

	public static JLabel newLabel(String str) {
		return newLabel(str, 0, 0);
	}
}
